﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace ExcelToChart
{
    public partial class Form_Chart : Form
    {
        DataTable chartDataSource;
        public Form_Chart()
        {
            InitializeComponent();
        }

        public Form_Chart(DataTable dt)
        {
            chartDataSource = dt;
            InitializeComponent();
        }

        private void Form_Chart_Load(object sender, EventArgs e)
        {
            DataTable dt = chartDataSource;

            int sc = chart1.Series.Count;

            if (dt.Columns.Count <= 1)
            {
                MessageBox.Show("源数据只有小于等于1列，无法生成图表");
                return;
            }

            chart1.Series[0].Name = dt.Columns[0].ColumnName;

            
            for (int i = sc; i < dt.Columns.Count; i++)
            {
                Console.WriteLine(dt.Columns[i].ColumnName);
                chart1.Series.Add(dt.Columns[i].ColumnName);
            }

            chart1.DataSource = dt;

            chart1.Series[0].XValueMember = dt.Columns[0].ColumnName;

            for(int i = 1; i < dt.Columns.Count; i++)
            {
                Console.WriteLine(dt.Columns[i].ColumnName);
                chart1.Series[i-1].YValueMembers = dt.Columns[i].ColumnName;   
            }


            /*string str = chart1.Series[0].YValueMembers.ToString();
            label1.Text = str;*/

            chart1.DataBind();
        }

        private DataTable CreateDataTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Date");
            dt.Columns.Add("Volume1");
            dt.Columns.Add("Volume2");
            dt.Columns.Add("Volume3");

            DataRow dr;

            dr = dt.NewRow();
            dr["Date"] = "Jan";
            dr["Volume1"] = 12333;
            dr["Volume2"] = 4111;
            dr["Volume3"] = 33;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Date"] = "Feb";
            dr["Volume1"] = 7333;
            dr["Volume2"] = 5111;
            dr["Volume3"] = 73;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Date"] = "Mar";
            dr["Volume1"] = 12333;
            dr["Volume2"] = 4111;
            dr["Volume3"] = 33;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Date"] = "Apr";
            dr["Volume1"] = 7333;
            dr["Volume2"] = 5111;
            dr["Volume3"] = 73;
            dt.Rows.Add(dr);

            return dt;

        }

        private void button_save_chart_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Bitmap (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg|EMF (*.emf)|*.emf|PNG (*.png)|*.png|SVG (*.svg)|*.svg|GIF (*.gif)|*.gif|TIFF (*.tif)|*.tif";
            sfd.FilterIndex = 2;
            sfd.RestoreDirectory = true;
            sfd.ShowDialog();

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                ChartImageFormat format = ChartImageFormat.Bmp;

                if (sfd.FileName.EndsWith("bmp"))
                {
                    format = ChartImageFormat.Bmp;
                }
                else if (sfd.FileName.EndsWith("jpg"))
                {
                    format = ChartImageFormat.Jpeg;
                }
                else if (sfd.FileName.EndsWith("emf"))
                {
                    format = ChartImageFormat.Emf;
                }
                else if (sfd.FileName.EndsWith("gif"))
                {
                    format = ChartImageFormat.Gif;
                }
                else if (sfd.FileName.EndsWith("png"))
                {
                    format = ChartImageFormat.Png;
                }
                else if (sfd.FileName.EndsWith("tif"))
                {
                    format = ChartImageFormat.Tiff;
                }
                /*else if (sfd.FileName.EndsWith("svg"))
                {
                    format = ChartImageFormat.Svg;
                }*/

                // Save image
                chart1.SaveImage(sfd.FileName, format);
            }
        }
    }
}
